package com.ruoyi.system.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.uuid.UUID;
import com.ruoyi.system.domain.ArTableFiledRelation;
import com.ruoyi.system.domain.SysFields;
import com.ruoyi.system.domain.SysTable;
import com.ruoyi.system.mapper.ArTableFiledRelationMapper;
import com.ruoyi.system.mapper.SysTableMapper;
import com.ruoyi.system.service.IArTableFiledRelationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.ArTableRelationMapper;
import com.ruoyi.system.domain.ArTableRelation;
import com.ruoyi.system.service.IArTableRelationService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 关系信息Service业务层处理
 * 
 * @author ruoyi_ln
 * @date 2022-03-01
 */
@Service
public class ArTableRelationServiceImpl implements IArTableRelationService 
{
    @Autowired
    private ArTableRelationMapper arTableRelationMapper;

    @Autowired
    private IArTableFiledRelationService arTableFiledRelationService;

    @Autowired
    private SysTableServiceImpl sysTableService;

    @Autowired
    private SysFieldsServiceImpl sysFieldsService;

    @Autowired
    private SysTableMapper sysTableMapper;

    /**
     * 查询关系信息
     * 
     * @param recNo 关系信息ID
     * @return 关系信息
     */
    @Override
    public ArTableRelation selectArTableRelationById(String recNo)
    {
        ArTableRelation arTableRelation = arTableRelationMapper.selectArTableRelationById(recNo);

        ArTableFiledRelation arTableFiledRelation = arTableRelation.getArTableFiledRelation();
        // 存入字段名
        SysFields sysFields = sysFieldsService.selectSysFieldsById(arTableFiledRelation.getParentFiled());
        arTableFiledRelation.setParentFiled(sysFields.getFieldAlias());

        SysFields sysFields1 = sysFieldsService.selectSysFieldsById(arTableFiledRelation.getChildFiled());
        arTableFiledRelation.setChildFiled(sysFields1.getFieldAlias());
        return arTableRelation;
    }

    /**
     * 查询关系信息列表
     * 
     * @param arTableRelation 关系信息
     * @return 关系信息
     */
    @Override
    public List<ArTableRelation> selectArTableRelationList(ArTableRelation arTableRelation)
    {

        List<ArTableRelation> arTableRelationList = arTableRelationMapper.selectArTableRelationList(arTableRelation);
        // 反显数据
        for (ArTableRelation tableRelation : arTableRelationList) {
            // 存入表名
            SysTable sysTable = sysTableService.selectSysTableById(tableRelation.getParentNo());
            tableRelation.setParentNo(sysTable.getTableAlias());

            SysTable sysTable1 = sysTableService.selectSysTableById(tableRelation.getChildNo());
            tableRelation.setChildNo(sysTable1.getTableAlias());

            ArTableFiledRelation arTableFiledRelation = tableRelation.getArTableFiledRelation();
            // 存入字段名
            SysFields sysFields = sysFieldsService.selectSysFieldsById(arTableFiledRelation.getParentFiled());
            arTableFiledRelation.setParentFiled(sysFields.getFieldAlias());

            SysFields sysFields1 = sysFieldsService.selectSysFieldsById(arTableFiledRelation.getChildFiled());
            arTableFiledRelation.setChildFiled(sysFields1.getFieldAlias());
        }
        return arTableRelationList;
    }

    /**
     * 新增关系信息
     * 
     * @param arTableRelation 关系信息
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public AjaxResult insertArTableRelation(ArTableRelation arTableRelation)
    {
        //新增表关系
        String id = UUID.randomUUIDPlus();
        arTableRelation.setRecNo(id);
        arTableRelation.setCreateBy(SecurityUtils.getUsername());
        arTableRelation.setCreateTime(DateUtils.getNowDate());
        arTableRelation.setDelFlag("0");
        arTableRelationMapper.insertArTableRelation(arTableRelation);

        //新增字段关系
        ArTableFiledRelation arTableFiledRelation = arTableRelation.getArTableFiledRelation();
        arTableFiledRelation.setRecNo(UUID.randomUUIDPlus());
        arTableFiledRelation.setRelationNo(id);
        arTableFiledRelation.setCreateBy(SecurityUtils.getUsername());
        arTableFiledRelation.setCreateTime(DateUtils.getNowDate());
        arTableFiledRelation.setDelFlag("0");
        arTableFiledRelationService.insertArTableFiledRelation(arTableFiledRelation);
        return AjaxResult.success();
    }

    /**
     * 修改关系信息
     * 
     * @param arTableRelation 关系信息
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public AjaxResult updateArTableRelation(ArTableRelation arTableRelation)
    {
        arTableRelation.setUpdateTime(DateUtils.getNowDate());
        arTableRelation.setUpdateBy(SecurityUtils.getUsername());
        arTableRelationMapper.updateArTableRelation(arTableRelation);
        // 修改字段关系
        ArTableFiledRelation arTableFiledRelation = arTableRelation.getArTableFiledRelation();
        arTableFiledRelation.setUpdateTime(DateUtils.getNowDate());
        arTableFiledRelation.setUpdateBy(SecurityUtils.getUsername());
        arTableFiledRelationService.updateArTableFiledRelation(arTableFiledRelation);
        return AjaxResult.success();
    }

    /**
     * 批量删除关系信息
     * 
     * @param recNos 需要删除的关系信息ID
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public AjaxResult deleteArTableRelationByIds(String[] recNos)
    {
        arTableRelationMapper.deleteArTableRelationByIds(recNos);
        // 删除字段关系
        arTableFiledRelationService.deleteArTableFiledRelationByIds(recNos);
        return AjaxResult.success();
    }

    /**
     * 删除关系信息信息
     * 
     * @param recNo 关系信息ID
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public AjaxResult deleteArTableRelationById(String recNo)
    {
        arTableRelationMapper.deleteArTableRelationById(recNo);
        // 删除字段关系
        arTableFiledRelationService.deleteArTableFiledRelationById(recNo);
        return AjaxResult.success();
    }

    @Override
    public ArTableRelation selectArTableRelationByTableId(String tableNo) {
        return arTableRelationMapper.selectArTableRelationByTableId(tableNo);
    }
}
